package com.yeswater.multithread.chapter1.test1_14;

import com.yeswater.multithread.chapter1.test1_12.test1.MyThread;
import org.junit.Test;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * author : yeswater
 * date : 2023/10/19
 */

public class RunTest {

    /**
     * 测试 线程优先级的继承性 priority
     */
    @Test
    public void test1() {
        System.out.println("main thread priority = " + Thread.currentThread().getPriority());
        // Thread.currentThread().setPriority(6);
        System.out.println("main thread priority = " + Thread.currentThread().getPriority());
        Thread t = new Thread(() -> {
            System.out.println("thread1 run priority = " + Thread.currentThread().getPriority());
            Thread t2 = new Thread(() -> {
                System.out.println("thread2 run priority = " + Thread.currentThread().getPriority());
            });
            t2.start();
        });
        t.start();
    }

    /**
     * 优先级较高的线程并不一定先执行完,具有随机性 不确定性
     */
    @Test
    public void test2() {
        for (int i = 0; i < 5; i++) {
            Thread t1 = new Thread(() -> {
                long begin = System.currentTimeMillis();
                long addResult = 0;
                for (int j = 0; j < 10; j++) {
                    for (int k = 0; k < 1000; k++) {
                        Random random = new Random();
                        random.nextInt();
                        addResult += k;
                    }
                }
                long end = System.currentTimeMillis();
                System.out.println("==== thread 1 use time = " + (end - begin));
            });
            t1.setPriority(10);
            t1.start();

            Thread t2 = new Thread(() -> {
                long begin = System.currentTimeMillis();
                long addResult = 0;
                for (int j = 0; j < 10; j++) {
                    for (int k = 0; k < 1000; k++) {
                        Random random = new Random();
                        random.nextInt();
                        addResult += k;
                    }
                }
                long end = System.currentTimeMillis();
                System.out.println("---- thread 2 use time = " + (end - begin));
            });
            t2.setPriority(1);
            t2.start();
        }
    }

    /**
     * 啥也没测出来...
     */
    @Test
    public void test3() throws InterruptedException {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();

        t1.setPriority(Thread.NORM_PRIORITY - 3);
        t2.setPriority(Thread.NORM_PRIORITY + 3);

        t1.start();
        t2.start();

        TimeUnit.SECONDS.sleep(20);
        t1.stop();
        t2.stop();

        System.out.println("t1 = " + t1.getCount());
        System.out.println("t2 = " + t2.getCount());
    }

    private static class MyThread extends Thread {
        private long count = 0;

        public long getCount() {
            return count;
        }

        @Override
        public void run() {
            while (true) {
                count++;
            }
        }
    }

}
